<template>
  <div class="coach-workload">
    <el-row :gutter="20">
      <!-- 统计卡片 -->
      <el-col :span="6" v-for="card in statisticsCards" :key="card.title">
        <el-card class="statistics-card" :body-style="{ padding: '20px' }">
          <div class="card-content">
            <div class="card-value">
              <span class="number">{{ card.value }}</span>
              <span class="unit">{{ card.unit }}</span>
            </div>
            <div class="card-title">{{ card.title }}</div>
            <div class="card-trend">
              <span :class="['trend', card.trend > 0 ? 'up' : 'down']">
                {{ Math.abs(card.trend) }}%
                <el-icon>
                  <CaretTop v-if="card.trend > 0" />
                  <CaretBottom v-else />
                </el-icon>
              </span>
              <span class="trend-text">较上月</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <!-- 工作量趋势图 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>工作量趋势</span>
              <div class="header-operations">
                <el-select v-model="selectedCoach" placeholder="选择教练" clearable style="width: 120px; margin-right: 16px;">
                  <el-option
                    v-for="coach in coaches"
                    :key="coach.id"
                    :label="coach.name"
                    :value="coach.id"
                  />
                </el-select>
                <el-radio-group v-model="trendTimeRange" size="small">
                  <el-radio-button label="week">本周</el-radio-button>
                  <el-radio-button label="month">本月</el-radio-button>
                  <el-radio-button label="year">本年</el-radio-button>
                </el-radio-group>
              </div>
            </div>
          </template>
          <div class="chart-container" ref="trendChartRef"></div>
        </el-card>
      </el-col>

      <!-- 工作量分布图 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>工作量分布</span>
              <el-select v-model="distributionType" size="small">
                <el-option label="按科目" value="subject" />
                <el-option label="按时段" value="timeSlot" />
                <el-option label="按学员类型" value="studentType" />
              </el-select>
            </div>
          </template>
          <div class="chart-container" ref="distributionChartRef"></div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <!-- 教练工作量排名 -->
      <el-col :span="24">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>教练工作量排名</span>
              <div class="header-operations">
                <el-select v-model="rankingType" style="width: 120px; margin-right: 16px;" size="small">
                  <el-option label="培训课时" value="trainingHours" />
                  <el-option label="学员数量" value="studentCount" />
                  <el-option label="完成率" value="completionRate" />
                </el-select>
                <el-select v-model="rankingTimeRange" size="small">
                  <el-option label="本周" value="week" />
                  <el-option label="本月" value="month" />
                  <el-option label="本年" value="year" />
                </el-select>
              </div>
            </div>
          </template>
          <el-table
            :data="rankingData"
            style="width: 100%"
            :max-height="400"
          >
            <el-table-column type="index" label="排名" width="80" />
            <el-table-column prop="name" label="教练姓名" width="120" />
            <el-table-column prop="type" label="教练类型" width="120">
              <template #default="{ row }">
                <el-tag :type="row.type === 'subject2' ? 'success' : 'warning'">
                  {{ getCoachTypeText(row.type) }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="trainingHours" label="培训课时" width="120" />
            <el-table-column prop="studentCount" label="学员数量" width="120" />
            <el-table-column prop="completionRate" label="完成率" width="120">
              <template #default="{ row }">
                {{ row.completionRate }}%
              </template>
            </el-table-column>
            <el-table-column prop="trend" label="趋势" width="120">
              <template #default="{ row }">
                <span :class="['trend', row.trend > 0 ? 'up' : 'down']">
                  {{ Math.abs(row.trend) }}%
                  <el-icon>
                    <CaretTop v-if="row.trend > 0" />
                    <CaretBottom v-else />
                  </el-icon>
                </span>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" min-width="200" show-overflow-tooltip />
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { CaretTop, CaretBottom } from '@element-plus/icons-vue'
import * as echarts from 'echarts'

// 统计卡片数据
const statisticsCards = [
  {
    title: '今日课时',
    value: 32,
    unit: '课时',
    trend: 15.6
  },
  {
    title: '本周课时',
    value: 186,
    unit: '课时',
    trend: 8.3
  },
  {
    title: '平均完成率',
    value: 96.5,
    unit: '%',
    trend: 2.1
  },
  {
    title: '学员满意度',
    value: 4.8,
    unit: '分',
    trend: 1.5
  }
]

// 教练列表
const coaches = [
  { id: '1', name: '李教练' },
  { id: '2', name: '王教练' },
  { id: '3', name: '张教练' },
  { id: '4', name: '赵教练' }
]

// 选中的教练
const selectedCoach = ref('')

// 图表相关
const trendTimeRange = ref('month')
const distributionType = ref('subject')
const rankingType = ref('trainingHours')
const rankingTimeRange = ref('month')

const trendChartRef = ref(null)
const distributionChartRef = ref(null)

let trendChart = null
let distributionChart = null

// 排名数据
const rankingData = ref([
  {
    name: '李教练',
    type: 'subject2',
    trainingHours: 186,
    studentCount: 45,
    completionRate: 98.5,
    trend: 15.6,
    remark: '教学经验丰富，学员反馈优秀'
  },
  {
    name: '王教练',
    type: 'subject3',
    trainingHours: 165,
    studentCount: 38,
    completionRate: 96.8,
    trend: 8.3,
    remark: '擅长处理疑难问题，耐心细致'
  },
  {
    name: '张教练',
    type: 'subject2',
    trainingHours: 158,
    studentCount: 36,
    completionRate: 95.5,
    trend: -2.1,
    remark: '新学员教学效果显著'
  },
  {
    name: '赵教练',
    type: 'subject3',
    trainingHours: 142,
    studentCount: 32,
    completionRate: 94.8,
    trend: 5.2,
    remark: '教学方法灵活，适应性强'
  }
])

// 获取教练类型文本
const getCoachTypeText = (type) => {
  const typeMap = {
    'subject2': '科目二教练',
    'subject3': '科目三教练',
    'theory': '理论教练'
  }
  return typeMap[type] || type
}

// 初始化趋势图
const initTrendChart = () => {
  trendChart = echarts.init(trendChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['培训课时', '学员数量']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: [
      {
        type: 'value',
        name: '培训课时',
        position: 'left'
      },
      {
        type: 'value',
        name: '学员数量',
        position: 'right'
      }
    ],
    series: [
      {
        name: '培训课时',
        type: 'line',
        smooth: true,
        data: [25, 28, 32, 30, 35, 28, 24]
      },
      {
        name: '学员数量',
        type: 'line',
        smooth: true,
        yAxisIndex: 1,
        data: [6, 8, 10, 8, 12, 8, 6]
      }
    ]
  }
  trendChart.setOption(option)
}

// 初始化分布图
const initDistributionChart = () => {
  distributionChart = echarts.init(distributionChartRef.value)
  const option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        name: '工作量分布',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 60, name: '科目二' },
          { value: 40, name: '科目三' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
  distributionChart.setOption(option)
}

// 监听窗口大小变化
const handleResize = () => {
  trendChart?.resize()
  distributionChart?.resize()
}

onMounted(() => {
  // 初始化图表
  initTrendChart()
  initDistributionChart()

  // 添加窗口大小变化监听
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  // 移除窗口大小变化监听
  window.removeEventListener('resize', handleResize)

  // 销毁图表实例
  trendChart?.dispose()
  distributionChart?.dispose()
})
</script>

<style scoped>
.coach-workload {
  padding: 20px;
}

.statistics-card {
  margin-bottom: 20px;
}

.card-content {
  text-align: center;
}

.card-value {
  margin-bottom: 8px;
}

.card-value .number {
  font-size: 24px;
  font-weight: bold;
  margin-right: 4px;
}

.card-value .unit {
  font-size: 14px;
  color: #909399;
}

.card-title {
  font-size: 14px;
  color: #606266;
  margin-bottom: 8px;
}

.card-trend {
  font-size: 12px;
}

.trend {
  display: inline-flex;
  align-items: center;
  margin-right: 4px;
}

.trend.up {
  color: #67c23a;
}

.trend.down {
  color: #f56c6c;
}

.trend-text {
  color: #909399;
}

.chart-row {
  margin-bottom: 20px;
}

.chart-card {
  height: 400px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-operations {
  display: flex;
  align-items: center;
}

.chart-container {
  height: 320px;
}
</style>